layui.use(['table', 'admin', 'ax', 'func','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 管理
     */
    var Reemploy = {
        tableId: "reemployTable"
    };


    /**
     * 一级区域点击事件
     */
    form.on('select(firstArea)', function (data) {
        let deptId = data.value;
        Reemploy.clearSelect('secondArea');
        Reemploy.clearSelect('thirdArea');
        Reemploy.clearSelect('fourArea');
        if (deptId != "") {
            let qyAjax = new $ax(Feng.ctxPath + "/dept/getRegion");
            qyAjax.set("pid", deptId);
            qyAjax.set("simpleName", "区域");
            let qyResult = qyAjax.start();
            $.each(qyResult.data, function (index, val) {
                $("#secondArea").append("<option value='" + val.deptId + "," + val.name + "'>" + val.name + "</option>");
            });
            form.render('select');
        }
    });
    /**
     * 二级区域点击事件
     */
    form.on('select(secondArea)', function (data) {
        let deptId = data.value;
        Reemploy.clearSelect('thirdArea');
        Reemploy.clearSelect('fourArea');
        if (deptId != "") {
            let qyAjax = new $ax(Feng.ctxPath + "/dept/getRegion");
            qyAjax.set("pid", deptId.split(",")[0]);
            qyAjax.set("simpleName", "区域");
            let qyResult = qyAjax.start();
            $.each(qyResult.data, function (index, val) {
                $("#thirdArea").append("<option value='" + val.deptId + "," + val.name + "'>" + val.name + "</option>");
            });
            form.render('select');
        }
    });
    /**
     * 三级区域点击事件
     */
    form.on('select(thirdArea)', function (data) {
        let deptId = data.value;
        Reemploy.clearSelect('fourArea');
        if (deptId != "") {
            let qyAjax = new $ax(Feng.ctxPath + "/dept/getRegion");
            qyAjax.set("pid", deptId.split(",")[0]);
            let qyResult = qyAjax.start();
            $.each(qyResult.data, function (index, val) {
                $("#fourArea").append("<option value='" + val.name + "'>" + val.name + "</option>");
            });
            form.render('select');
        }
    });
    /**
     * select下拉框清空事件
     */
    Reemploy.clearSelect = function(selectId){
        $("#" + selectId).html("");
        $("#" + selectId).append("<option value=''>全部</option>")
        form.render('select');
    }

    /**
     * 初始化表格的列
     */
    Reemploy.initColumn = function () {
        return [[
            {type: 'checkbox', fixed: true},
            {field: 'id', align: "left", hide: true, sort: true, title: 'id'},
            {field: 'reempName', align: "left", title: '姓名',width:90, fixed: true},
            {field: 'reempNum', align: "left", title: '编号',minWidth:150},
            {field: 'appointment', align: "left", title: '职务',minWidth:85},
            {field: 'telephone', align: "left", title: '电话',width: 120},
            {field: 'identifyCard', align: "left", title: '身份证号',width:180},
            {field: 'entryDate', align: "left", title: '入职日期',width: 110},
            {field: 'originalEntryStore', align: "left", title: '原入职门店',width: 180},
            {field: 'originalOperationsManagerInfo', align: "left", title: '原运营经理姓名/电话',width: 180},
            {field: 'entryStore', align: "left", title: '现入职门店',width:180},
            {field: 'operationsManagerInfo', align: "left", title: '现运营经理姓名/电话',width:180},
            {field: 'firstArea', align: "left", title: '一级区域',width:90},
            {field: 'secondArea', align: "left", title: '二级区域',width:90}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Reemploy.tableId,
        url: Feng.ctxPath + '/btyReemploy/list',
        page: true,
        limit: 20,
        height: "full-59",
        cellMinWidth: 100,
        cols: Reemploy.initColumn(),
        done:function (res, pageNo) {
            if(res.count > 0){
                let reemployDate = res.data;
                for(let i = 0; i < reemployDate.length; i++){
                    reemployDate[i]['dataIndex'] = pageNo + ',' + (i + 2 + reemployDate.length) + ',' + reemployDate[i].id;
                }
                Reemploy.curPageNo = pageNo;
                Reemploy.checkboxSelect(res, pageNo);
            }
        }
    });

    Reemploy.selectDataArr = [];
    Reemploy.curPageNo = 1;

    Reemploy.checkboxSelect = function(){
        let tempArr = JSON.parse(JSON.stringify(Reemploy.selectDataArr));
        for(let s = 0; s < tempArr.length; s++){
            if(tempArr[s].dataIndex.indexOf(Reemploy.curPageNo + ",") == 0){
                let dataTempArr = tempArr[s].dataIndex.split(",");
                document.getElementsByClassName('layui-unselect layui-form-checkbox')[dataTempArr[1]].click();
            }
        }
    }

    /**
     * 重置按钮事件
     */
    $('#btnReset').click(function () {
        $('input').val('');
        $("#firstArea").val('');
        $("#secondArea").val('');
        $("#thirdArea").val('');
        $("#fourArea").val('');
        form.render();
    });

    /**
     * 搜索按钮点击事件
     */
    $('#btnSearch').click(function () {
        Reemploy.search();
    });

    /**
     * 点击查询按钮
     */
    Reemploy.search = function () {
        Reemploy.selectDataArr = [];
        let queryData = {};
        queryData['nameOrNumOrCard'] = $("#nameOrNumOrCard").val();
        queryData['firstArea'] = $("#firstArea").val();
        let secondArea = $("#secondArea").val();
        if(secondArea != "" && secondArea.indexOf(",") != -1){
            queryData['secondArea'] = secondArea.split(",")[1];
        }else{
            queryData['secondArea'] = $("#secondArea").val();
        }
        let thirdArea = $("#thirdArea").val();
        if(thirdArea != "" && thirdArea.indexOf(",") != -1){
            queryData['thirdArea'] = thirdArea.split(",")[1];;
        }else{
            queryData['thirdArea'] = $("#thirdArea").val();
        }
        queryData['fourArea'] = $("#fourArea").val();
        table.reload(Reemploy.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 删除按钮点击事件
     */
    $('#btnBatchDel').click(function () {
        Reemploy.onBatchDeleteItem();
    });

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Reemploy.onBatchDeleteItem = function () {
        if(Reemploy.selectDataArr.length == 0){
            Feng.error("请选择要删除的记录！！！");
            return;
        }
        let reemployIds = [];
        Reemploy.selectDataArr.forEach(function (item,i) {
            let dataIndexArr = item.dataIndex.split(',');
            reemployIds.push(dataIndexArr[2]);
        });
        Reemploy.selectDataArr = [];
        let operation = function () {
            let ajax = new $ax(Feng.ctxPath + "/btyReemploy/batchdelete", function (data) {
                Feng.success("删除成功!");
                Reemploy.search();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("reemployIds", reemployIds.join(','));
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    /**
     * checkbox点击事件
     */
    table.on('checkbox()', function(obj){
        let checkedArr = table.checkStatus('reemployTable').data;
        Reemploy.selectDataArr = Reemploy.selectDataArr.filter(item => (item.dataIndex.indexOf(Reemploy.curPageNo + ',') != 0)).concat(checkedArr);
    });

});
